jQuery(function($) {
	var doc = document,
		$doc = $(doc),
		M = {};

		//工具函数
	var utils = {
		//检测类型
		type: function(obj) {
			return Object.prototype.toString.call(obj).slice(8, -1);
		},
		
		//表单值是否为空
		empty: function(val) {
			if (val === "") {
				return true;
			} else {
				return false;
			}
		},
		
		//是否为数字
		isNumber: function(num) {
			var reg = /^\d+$/;
			return reg.test(num);
		},
		
		//输出错误信息
		errorMessage: function(node, message) {
			if (node[0].nodeType === 1) {
				node.show();
				node.html(message);
			} else {
				throw "errorMessage <node> 不是元素节点！";
			}
		},
		errMessage: function(node) {
			if (utils.type(node) === "String") {
				$(node).css({"visibility": "visible"});
			} else {
				throw "errorMessage <node> 不是字符串！";
			}
		},
		//清除错误信息
		clearErrMessage: function(node) {
			//if (node[0].nodeType === 1) {
				node.hide().html("");
			//}
		},
		
		//检查手机号
		checkMoblie: function(elm) {
			var tel = /^(0|86|17951)?(13[0-9]|15[012356789]|17[678]|18[0-9]|14[57])[0-9]{8}$/g;
			
			if ( elm !== undefined && elm[0].nodeType === 1 ) {
				if (!tel.test(elm.val())) {
					return false;
				}
				return true;
			}
		},
		
		//检测邮箱
		checkEmail: function(elm) {
			var reg = /([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})/;
			
			if (utils.type(elm) === "Object") {
				if (!reg.test(elm.val())) {
					return false;
				}
				return true;
			}
			return false;
		},

		checkId: function(elm) {
			var reg = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/;

			if (utils.type(elm) === "Object") {
				if (!reg.test(elm.val())) {
					return false;
				}
				return true;
			}
			return false;
		},

		checkQQ: function(val) {
			var reg = /^[1-9]\d{4,12}$/;

			if (!reg.test(val)) {
				return false;
			} else {
				return true;
			}
		},
		
		scroll: function() {
			return {
				left: doc_.body.scrollLeft || doc_.documentElement.scrollLeft,
				top: doc_.body.scrollTop || doc_.documentElement.scrollTop
			};
		},
		
		offset: function() {
			return {
				width: doc_.body.offsetWidth || doc_.documentElement.offsetWidth,
				height:  doc_.body.offsetHeight || doc_.documentElement.offsetHeight
			};
		},
		getViewport: function() {
			if (doc_.compatMode == "BackCompat") {
				return {
					width: doc_.body.clientWidth,
					height: doc_.body.clientHeight
				};
			} else {
				return {
					width: doc_.documentElement.clientWidth,
					height: doc_.documentElement.height
				};
			}
		}
	};
	window.utils = utils;

	//取消button默认行为
	doc.addEventListener("click", function(event) {
		var target = event.target,
			tagName = target.tagName;
			
		if (tagName === "BUTTON") {
			event.preventDefault();
		}
	}, false);

	//插入页头和页尾
	function Ajax() {
		return new XMLHttpRequest();
	}
	function insertHeader(node, text) {
		node.innerHTML = text;
	}

	function getHtml(node, link) {

		if (node === null || node.nodeType !== 1) {
			return;
		}

		var xhr = Ajax();

		if (!xhr) {
			throw("xhr error");
			return;
		}

		xhr.onreadystatechange = function() {
			if (xhr.readyState == 4) {
				if ((xhr.status >= 200 && xhr.status < 300) || xhr.status == 304) {
					insertHeader(node, xhr.responseText);
				} else {
					console.log("Request was unsucdessful: " + xhr.status);
				}
			}
		}
		xhr.open("get", link, true);
		xhr.send();
	};
	
	getHtml(doc.querySelector(".serviceHeader"), "../wike/facilitatorheader.html");
	getHtml(doc.querySelector(".footer"), "../wike/footer.html");
	getHtml(doc.querySelector(".mainHeader"), "../wike/mainHeader.html");
});